// 电池包监控数据类型和接口
export interface BatteryPackItem {
  id?: number;                    // 电池包ID
  batteryPackSn: string;          // 电池包序列号
  deviceSn: string;               // 设备序列号
  batteryType: string;            // 电池类型
  capacity: number;               // 容量
  faultStatus: "Normal" | "Faulty"; // 故障状态
  details?: string;               // 详细信息（可选）
  batteryCode?: string;           // 电池代码（对应batteryPackSn，用于API调用）
  deviceId?: number;              // 设备ID（对应deviceSn，用于API调用）
}

// 实时数据接口定义
export interface BatteryPackRealTimeData {
  basicInfo: {
    batteryPackSn: string;
    deviceSn: string;
    faultStatus: string;
    batteryType: string;
    capacityKwh: number;
  };
  realTimeMonitoring: {
    stateOfCharge: string;
    batteryPackSOC: number;
    batteryPack: {
      totalVoltage: number;
      totalCurrent: number;
      currentDirection: string;
      temperature: number;
      unit: {
        voltage: string;
        current: string;
        temperature: string;
      };
    };
  };
  cellAnalysis: {
    highestVoltage: {
      value: number;
    };
    lowestVoltage: {
      value: number;
    };
  };
}

// API响应接口
export interface BatteryRealTimeResponse {
  code: number;
  msg: string;
  data: {
    batteryPackReal: BatteryPackRealTimeData;
  };
}

// 电池类型列表（这些值应该从API获取，这里提供默认值作为备用）
const defaultBatteryTypes = ["AB1000", "AB2000", "AB3000", "LFP6000", "LFP8000"];

// 故障状态
type FaultStatus = "Normal" | "Faulty";
const faultStatuses: FaultStatus[] = ["Normal", "Faulty"];

// 生成随机SN码
function generateRandomSN(): string {
  return (
    "10000" +
    Math.floor(Math.random() * 10000000000)
      .toString()
      .padStart(10, "0")
  );
}

// 生成随机容量值，保留两位小数
function generateRandomCapacity(): number {
  return parseFloat((Math.random() * 5 + 1).toFixed(2));
}

// 生成模拟的电池包数据
function generateMockBatteryPacks(count: number): BatteryPackItem[] {
  const batteryPacks: BatteryPackItem[] = [];

  for (let i = 0; i < count; i++) {
    const serialNumber = generateRandomSN();
    const batteryType =
      defaultBatteryTypes[Math.floor(Math.random() * defaultBatteryTypes.length)];
    const faultStatus =
      faultStatuses[Math.floor(Math.random() * faultStatuses.length)];

    batteryPacks.push({
      id: 1000000 + i, // 生成模拟ID
      batteryPackSn: serialNumber,
      deviceSn: serialNumber, // 使用相同的SN作为设备SN
      batteryType: batteryType,
      capacity: generateRandomCapacity(),
      faultStatus: faultStatus,
      details: "Details",
      batteryCode: serialNumber, // 使用电池包序列号作为batteryCode
      deviceId: parseInt(serialNumber.replace(/\D/g, '').slice(-8)) || (1000000 + i) // 从设备SN提取数字作为deviceId
    });
  }

  return batteryPacks;
}

// 生成85条模拟数据
export const mockBatteryPacks = generateMockBatteryPacks(85);

// 导出所有故障状态的选项
export const faultStatusOptions = [
  { value: "All fault status", label: "All fault status" },
  { value: "Normal", label: "Normal" },
  { value: "Faulty", label: "Faulty" }
];

// 导出所有电池类型的选项（使用默认值，实际应该从API获取）
export const batteryTypeOptions = [
  { value: "All battery type", label: "All battery type" },
  ...defaultBatteryTypes.map(type => ({ value: type, label: type }))
];

// 导出默认对象
export default {
  mockBatteryPacks,
  faultStatusOptions,
  batteryTypeOptions
};
